<template>
  <div class="upload-container">
     <el-button
      :style="{background:color,borderColor:color}"
      icon="el-icon-upload"
      size="mini"
      type="primary"
      @click=" dialogVisible=true"
    >上传</el-button>
    <!-- <el-button
      style="text-align: center;"
      size="mini"
      type="primary"
      @click=" dialogVisible1=true"
    >上传视频</el-button> -->


    <el-dialog :visible.sync="dialogVisible" append-to-body >
      <el-upload
        :multiple="true"
        :file-list="fileList"
        :show-file-list="true"
        :modal="false"
        :on-remove="handleRemove"
        :on-success="handleSuccess"
        :before-upload="beforeUpload"
        class="editor-slide-upload"
        :action="uploadImgUrl"
        :headers="headers"
        list-type="picture-card"
      >
        <!--  action="https://httpbin.org/post"  -->
        <el-button size="small" type="primary">
          上传 图片
        </el-button>
      </el-upload>
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 认</el-button>
    </el-dialog>

<!--    <el-dialog :visible.sync="dialogVisible1">-->
<!--      <el-upload-->
<!--        :multiple="true"-->
<!--        :file-list="fileList"-->
<!--        :limit="1"-->
<!--        :trigger="triggerdas"-->
<!--        :on-error="failuretriggerdas"-->
<!--        :show-file-list="true"-->
<!--        :on-remove="handleRemove1"-->
<!--        :on-success="handleSuccess1"-->
<!--        class="editor-slide-upload upload-demo"-->
<!--        action="http://app.daicangzhicheng.com:8080/ctl/user_real/uploadREAL.action"-->
<!--        style=" margin-bottom: 20px;"-->
<!--        :on-preview="beforeUploadVideo"-->
<!--      >-->
<!--        <el-button size="small" type="primary">点击上传</el-button>-->


<!--        <div slot="tip" style="  margin-left: 20px;" class="el-upload__tip">请注意视频不能超过5M大小</div>-->
<!--      </el-upload>-->

<!--      <el-button @click="dialogVisible1 = false">取 消</el-button>-->
<!--      <el-button type="primary" @click="dialogVisible1 = false">确 认</el-button>-->
<!--    </el-dialog>-->
  </div>
</template>

<script>
import {getToken} from "@/utils/auth";

export default {
  name: "EditorSlideUpload",
  props: {
    color: {
      type: String,
      default: "#1890ff"
    }
  },
  data() {
    return {
      dialogVisible: false,
      dialogVisible1: false,
      // uploadImgUrl: "https://www.youhuiniuapp.com/manage/common/upload", // 上传的图片服务器地址
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      listObj: {},
      fileList: [],
      triggerdas:"11123132",
      paste:''
    };
  },
  methods: {


//数据处理
handleData(val){
  console.log(val)
  // this.message = this.message + '七味都气d ' + val
},


    handleRemove1(){},
    handleSuccess1(res, file){
      console.log(this.$copyText,"123132")

      if(res.code==200){
        this.$message({
           showClose: true,
          message: '上传成功,点击文件复印链接,并在文本框里点击视频粘贴地址',
          type: 'success',
          offset:100,
          duration:0
        });
        this.paste=res.data
        console.log(this.paste,"1111")
      }else{
        this.$message({
          showClose: true,
          message: e.msg,
          type: 'error'
        });
      }
        this.$copyText(this.paste).then(function (e) {
          console.log(e,"111")
        }, function (e) {
          console.log(e,"222")
        })
    },
    failuretriggerdas(err, file, fileList){
      this.$message({
          showClose: true,
          message: '上传失败,可能是视频太大了',
          type: 'error'
        });
    },

    beforeUploadVideo(file) {
     console.log(file)
      this.$copyText(this.paste).then(function (e) {
          console.log(e,"111")
        }, function (e) {
          console.log(e,"222")
        })
    },
    uploadVideoProcess(event, file, fileList) {
      this.videoFlag = true;
      this.videoUploadPercent = file.percentage.toFixed(0);
    },
    checkAllSuccess() {
      return Object.keys(this.listObj).every(
        item => this.listObj[item].hasSuccess
      );
    },
    handleSubmit() {
      const arr = Object.keys(this.listObj).map(v => this.listObj[v]);
      // console.log(arr,"arr")
      if (!this.checkAllSuccess()) {
        this.$message(
          "请等待所有图像成功上传。 如果出现网络问题，请刷新页面，然后重新上传！"
        );
        return;
      }
      this.$emit("successCBK", arr);
      this.listObj = {};
      this.fileList = [];
      this.dialogVisible = false;
    },
    // 上传服务器成功的
    handleSuccess(response, file) {
      const uid = file.uid;
      const objKeyArr = Object.keys(this.listObj);
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          this.listObj[objKeyArr[i]].url = response.data.url;
          this.listObj[objKeyArr[i]].hasSuccess = true;
          return;
        }
      }
    },
    handleRemove(file) {
      const uid = file.uid;
      const objKeyArr = Object.keys(this.listObj);
      for (let i = 0, len = objKeyArr.length; i < len; i++) {
        if (this.listObj[objKeyArr[i]].uid === uid) {
          delete this.listObj[objKeyArr[i]];
          return;
        }
      }
    },
    beforeUpload(file) {
      const _self = this;
      const _URL = window.URL || window.webkitURL;
      const fileName = file.uid;
      this.listObj[fileName] = {};
      return new Promise((resolve, reject) => {
        const img = new Image();
        img.src = _URL.createObjectURL(file);
        img.onload = function() {
          _self.listObj[fileName] = {
            hasSuccess: false,
            uid: file.uid,
            width: this.width,
            height: this.height
          };
        };
        resolve(true);
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.editor-slide-upload {
  margin-bottom: 20px;

  ::v-deep  .el-upload--picture-card {
    width: 100%;

  }
}
::v-deep  .el-dialog{
  z-index: 2005 !important;
  margin-top: 22vh !important;
}
</style>
